package com.yanqu.road.dao.impl.activity.selectlottery;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.selectlottery.data.SelectLotteryJcUserData;
import com.yanqu.road.entity.activity.selectlottery.data.SelectLotteryNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectLotteryNoticeDataDaoImpl extends TempDataObjectDao<SelectLotteryNoticeData> {

    public static String getSelectLotteryNoticeDataSql = "select * from `t_c_activity_select_lottery_notice_data` where `activityId`=? limit ?";
    public List<SelectLotteryNoticeData> getSelectLotteryNoticeData(int activityId, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            List<SelectLotteryNoticeData> list = new ArrayList<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSelectLotteryNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                SelectLotteryNoticeData data = parse(rs);
                list.add(data);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    @Override
    protected boolean updateData(SelectLotteryNoticeData selectLotteryNoticeData) {
        return false;
    }

    public static String insertSelectLotteryNoticeDataSql = "insert into `t_c_activity_select_lottery_notice_data`(activityId,userId,serverId,rewardId,nickName,time) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(SelectLotteryNoticeData selectLotteryNoticeData) {
        selectLotteryNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryNoticeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryNoticeData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryNoticeData.getRewardId()));
        para.put(i++, new DbParameter(Types.VARCHAR, selectLotteryNoticeData.getNickName()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryNoticeData.getTime()));
        if (getDb().executeNoneQuery(insertSelectLotteryNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SelectLotteryNoticeData parseData(ResultSet rs) throws SQLException {
        SelectLotteryNoticeData selectLotteryNoticeData = new SelectLotteryNoticeData();
        selectLotteryNoticeData.setActivityId(rs.getInt("activityId"));
        selectLotteryNoticeData.setUserId(rs.getLong("userId"));
        selectLotteryNoticeData.setServerId(rs.getInt("serverId"));
        selectLotteryNoticeData.setRewardId(rs.getInt("rewardId"));
        selectLotteryNoticeData.setNickName(rs.getString("nickName"));
        selectLotteryNoticeData.setTime(rs.getLong("time"));
        return selectLotteryNoticeData;
    }
}
